<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     * 
     */
    public function index()
    {
        $this->success('请求成功');
    }


    /**
     * 根据当前节点获取所有父id
     */
    public function getParents(){
        set_time_limit(0);
        //$uid = input("uid");
        $type = input("type");
        if($type==1){
            $table = "user_tree";
        }elseif($type==2){
            $table = "user_tree_km";
        }
        $parentList = Db::name($table)->where("node_id","not null")->select();
        foreach ($parentList as &$val) {
            $userTree = Db::name($table)->where("uid", $val["uid"])->find();
            $tree_where["tree_id"] = $userTree["tree_id"];//根据当前树
            $tree_where["lft"] = array("<", $userTree["lft"]);//左节点小于
            $tree_where["rgt"] = array(">", $userTree["rgt"]);//大于右节点              -------------------根据level倒叙选两个
            $parent_list = Db::name($table)->where("lft", "<", $userTree["lft"])->where("rgt", ">", $userTree["rgt"])->field("uid,username")->order("level DESC")->select();
            //halt($parent_list);
            $str = "";
            foreach ($parent_list as &$value) {
                $str .= "=>" . $value["username"];
            }
            echo $str."                    level:".$userTree["level"]."</br>";
        }
    }

    /**
     * 根据当前节点获取所有父id
     */
//    public function getChilds(){
//
//        $uid = input("uid");
//        $type = input("type");
//        if($type==1){
//            $table = "user_tree";
//        }elseif($type==2){
//            $table = "user_tree_km";
//        }
//        $userTree = Db::name($table)->where("uid",$uid)->find();
//        $tree_where["tree_id"] = $userTree["tree_id"];//根据当前树
//        $tree_where["lft"] = array("<",$userTree["lft"]);//左节点小于
//        $tree_where["rgt"] = array(">",$userTree["rgt"]);//大于右节点              -------------------根据level倒叙选两个
//        $parent_list = Db::name($table)->where("lft",">",$userTree["lft"])->where("rgt","<",$userTree["rgt"])->field("uid,username")->order("level DESC")->select();
//        //halt($parent_list);
//        $str = "";
//        foreach ($parent_list as &$value){
//            $str .=  "=>".$value["username"];
//        }
//        echo $str;
//    }



    /**
     * 跑断腿
     */
     public function getTree(){
         set_time_limit(0);
         $userTreeList = Db::name("user_tree")->where("node_id","not null")->select();
         foreach ($userTreeList as &$value){
             if($value["parent_id"]==0){
                 $initPoit = [
                     "lft"=>1,
                     "rgt"=>2,
                     "username"=>$value["username"],
                     "tree_id"=>1,
                     "mobile"=>$value["mobile"],
                     "on_time"=>$value["on_time"],
                     "level"=>0,
                     "parent_id"=>0,
                     "parent_name"=>"",
                     "uid"=>$value["uid"]
                 ];
                 Db::name("user_tree_km")->insert($initPoit);
             }
             if($value["parent_id"]>0){
                 $parentTree = Db::name("user_tree_km")->where("uid",$value["parent_id"])->find();//找到父树
                 Db::name("user_tree_km")->where("rgt",">",$parentTree["lft"])->where("tree_id",$parentTree["tree_id"])->setInc("rgt",2);//更新这课树右节点条件是rgt>lft 加2
                 Db::name("user_tree_km")->where("lft",">",$parentTree["lft"])->where("tree_id",$parentTree["tree_id"])->setInc("lft",2);//更新这课树左节点条件是lft>lft 加2
                 $secData = [
                     "uid"=>$value["uid"],
                     "username"=>$value["username"],
                     "parent_id"=>$value["parent_id"],
                     "parent_name"=>$value["parent_name"],
                     "on_time"=>$value["on_time"],
                     "mobile"=>$value["mobile"] ,
                     "tree_id"=>$parentTree["tree_id"],
                     "lft"=>$parentTree["lft"]+1,
                     "rgt"=>$parentTree["lft"]+2,
                     "level"=>$parentTree["level"]+1
                 ];
                 Db::name("user_tree_km")->insert($secData);
             }
         }
     }

}
